package com.cloudfun.campusshare.util;

import org.apache.commons.lang3.ArrayUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * Created by Huqin on 2020/1/17
 */
public class EnumIntegerValidator implements ConstraintValidator<EnumInteger, Integer> {

    /**
     * 可选值
     */
    int[] values;

    @Override
    public void initialize(EnumInteger constraintAnnotation) {
        values = constraintAnnotation.values();
    }

    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        if (null != value && ArrayUtils.isNotEmpty(values)) {
            return ArrayUtils.contains(values, value);
        }
        return true;
    }
}
